import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        time: "验证码",
        currentTime: 60,
        lable: false,
        phone: "",
        numberPhone: +"",
        otpCode: "",
    },
    form: {
        sex: "1", //默认是男
        birthday: "", //默认日期
        phone: "", //手机号
        otpCode: "", //验证码
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.userNameInfoPost();
    },
    bindchangePhone(e) {
        console.log(e);
        this.setData({
            numberPhone: e.detail.value,
        });
        this.form.phone = e.detail.value;
    },
    validation(e) {
        this.setData({
            otpCode: e.detail.value,
        });
        this.form.otpCode = e.detail.value;
    },
    // 发送验证码
    async accomplish() {
        if (this.data.numberPhone.length != 11) {
            wx.showToast({
                title: "请输入正确的手机号",
                icon: "none",
            });
            return;
        } else {
            var that = this;
            var currentTime = that.data.currentTime;
            var interval;
            that.setData({
                lable: true,
                time: currentTime + " S",
                disabled: true,
            });
            interval = setInterval(function() {
                that.setData({
                    time: currentTime - 1 + " S",
                });
                currentTime--;
                if (currentTime <= 0) {
                    clearInterval(interval);
                    that.setData({
                        time: "重新获取",
                        currentTime: 60,
                        disabled: false,
                    });
                }
            }, 1000);
            let res = await request({
                url: "common/sendSMSCode?telephone=" + this.data.numberPhone,
            });
            console.log(res);
        }
    },
    //用户信息
    async userNameInfoPost() {
        let res = await request({ url: "User/findById" });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                phone: res.data.phone || "暂无",
            });
        }
    },
    // 完成按钮
    async phoneBtn() {
        if (this.data.phone == "") {
            wx.showToast({
                title: "请输入手机号",
                icon: "none",
            });
            return;
        } else if (this.data.otpCode == "") {
            wx.showToast({
                title: "请输入验证码",
                icon: "none",
            });
            return;
        }
        let res = await request({
            url: "User/update",
            data: this.form,
        });
        console.log(res);
        if (res.code == 200) {
            wx.switchTab({
                url: "../myName/index",
            });
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});